﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using AllenalaMente.State;
using Mogade;
using AllenalaMente.MogadeCode;

namespace AllenalaMente.Games
{
    public partial class GamesFinalRecap : UserControl
    {
        private GameState gameState;

        public bool isAnimated = true;

        private DispatcherTimer timer = new DispatcherTimer();
        private int score = 0, finalScore = 0;

        private float Time;
        private double size = 0.01;
        private bool calculate = true;
        private Point backgroundPosition = new Point(308, 108);

        public GamesFinalRecap(GameEventArgs[] args)
        {
            InitializeComponent();

            buttonClose2.Content = Locale.Strings.ButtonBack;
            buttonSubmit.Content = Locale.Strings.Submit;
            textBlockNewRecord.Text = Locale.Strings.NewRecord;
            textBlockRecordLogic.Text = Locale.Strings.NewRecord;
            textBlockRecordMath.Text = Locale.Strings.NewRecord;
            textBlockRecordMemory.Text = Locale.Strings.NewRecord;
            textBlockRecordReflex.Text = Locale.Strings.NewRecord;

            gameState = GameStateManager.GetState();

            CalculateScores(args);

            // Timer
            timer.Interval = TimeSpan.FromMilliseconds(10);
            timer.Tick += new EventHandler(Update);
            timer.Start();
        }

        private void SetImage(Image image, string ImagePath)
        {
            // Carica Immagine
            Uri uri = new Uri("/AllenalaMente;component/" + ImagePath, UriKind.Relative);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            image.SetValue(Image.SourceProperty, img);
        }

        private void CalculateScores(GameEventArgs[] args)
        {
            for (int i = 0; i < 4; i++)
            {
                if (args != null)
                {
                    if (args[i] != null)
                    {
                        int score = args[i].score;
                        int index = args[i].index;

                        gameState.GamesList[index].score = score;

                        if (score > gameState.GamesList[index].recordScore)
                        {
                            gameState.GamesList[index].recordScore = score;

                            showNewRecord(i);
                        }

                        switch (i)
                        {
                            case 0:
                                SetImage(imageGameMemory, gameState.GamesList[args[i].index].iconPath);
                                break;

                            case 1:
                                SetImage(imageGameMath, gameState.GamesList[args[i].index].iconPath);
                                break;

                            case 2:
                                SetImage(imageGameReflex, gameState.GamesList[args[i].index].iconPath);
                                break;

                            case 3:
                                SetImage(imageGameLogic, gameState.GamesList[args[i].index].iconPath);
                                break;
                        }

                        setText(i, "" + score);
                    }
                }
            }

            if (args != null)
            {
                if (args[0] != null && args[1] != null && args[2] != null && args[3] != null)
                    finalScore = (args[0].score + args[1].score + args[2].score + args[3].score);
            }

            // Nuovo record
            if (finalScore > gameState.BestScore)
            {
                gameState.BestScore = finalScore;
                buttonClose.Visibility = Visibility.Collapsed;
                buttonClose2.Visibility = Visibility.Visible;
                buttonSubmit.Visibility = Visibility.Visible;
                textBlockNewRecord.Visibility = Visibility.Visible;
                canvasNewRecord.Visibility = Visibility.Visible;
            }
        }

        private void Update(object sender, EventArgs e)
        {
            Time++;

            if (calculate && Time > 50)
            {
                size += 0.05;

                if (size > 200) size = 0.0;

                imageBackground.Width += size;
                imageBackground.Height += size;

                backgroundPosition.X -= (size / 2);
                backgroundPosition.Y -= (size / 2);

                Canvas.SetLeft(imageBackground, backgroundPosition.X);
                Canvas.SetTop(imageBackground, backgroundPosition.Y);

                imageBackground.Opacity += 0.05;
                if (imageBackground.Opacity > 0.7) imageBackground.Opacity = 0.7;

                // Score

                score += (int)Time;

                if (score >= finalScore)
                {
                    imageBackground.Opacity = 0.7;

                    score = finalScore;

                    calculate = false;

                    timer.Tick -= Update;

                    isAnimated = false;
                }

                textBlockTotal.Text = "" + score;
            }
        }

        /// <summary>
        /// Salta l'animazione di calcolo dei Punteggi
        /// </summary>
        public void SkipAnimation()
        {
            score = finalScore;

            imageBackground.Width = 300;
            imageBackground.Height = 225;

            backgroundPosition.X = 250;
            backgroundPosition.Y = 70;

            Canvas.SetLeft(imageBackground, backgroundPosition.X);
            Canvas.SetTop(imageBackground, backgroundPosition.Y);

            imageBackground.Opacity = 0.7;

            textBlockTotal.Text = "" + finalScore;
        }

        private void setText(int index, string text)
        {
            switch (index)
            {
                case 0:
                    textBlockMemory.Text = text;
                    break;

                case 1:
                    textBlockMath.Text = text;
                    break;

                case 2:
                    textBlockReflex.Text = text;
                    break;

                case 3:
                    textBlockLogic.Text = text;
                    break;
            }
        }

        private void showNewRecord(int index)
        {
            switch (index)
            {
                case 0:
                    textBlockRecordMemory.Visibility = Visibility.Visible;
                    break;

                case 1:
                    textBlockRecordMath.Visibility = Visibility.Visible;
                    break;

                case 2:
                    textBlockRecordReflex.Visibility = Visibility.Visible;
                    break;

                case 3:
                    textBlockRecordLogic.Visibility = Visibility.Visible;
                    break;
            }
        }

        private void buttonSubmit_Click(object sender, RoutedEventArgs e)
        {
            Pages.LeaderboardPage leaderboardPage = new Pages.LeaderboardPage();
            leaderboardPage.Submit_Click(this, null);

            buttonSubmit.Visibility = Visibility.Collapsed;
            buttonClose2.Visibility = Visibility.Collapsed;

            buttonClose.Visibility = Visibility.Visible;
        }
    }
}
